<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:composition template="/template.xhtml">
	<ui:define name="corpo">
		<h:form id="cadForm">
			<h2>Usuários</h2>
			<p:growl id="growl" life="2500"></p:growl>
			<p:commandButton value="Novo cadastro"
				oncomplete="popupCadastro.show()" />
			<p:dialog widgetVar="popupCadastro" header="Cadastro de usuário"
				closable="false" resizable="false" modal="true" hideEffect="slide"
				showEffect="clip" style="width:32px;height:32px">

				<h:panelGrid id="panelusr" columns="3" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Nome:" for="txtNome" />
					<p:inputText id="txtNome" value="#{usuarioBean.usuario.nome}">
						<p:ajax event="keyup" />
					</p:inputText>
					<h:outputText></h:outputText>

					<h:outputLabel value="Sobrenome:" for="txtSobrenome" />
					<p:inputText id="txtSobrenome"
						value="#{usuarioBean.usuario.sobrenome}">
						<p:ajax event="blur" update="txtSugestao" />
					</p:inputText>
					<h:outputText></h:outputText>

					<h:outputLabel value="Matricula:" for="txtMatricula" />
					<p:inputText id="txtMatricula"
						value="#{usuarioBean.usuario.matricula}" />
					<h:outputText></h:outputText>

					<h:outputLabel value="Perfil:" for="selectPerfil" />
					<p:selectOneRadio id="selectPerfil"
						value="#{usuarioBean.usuario.perfil}">
						<f:selectItems value="#{usuarioBean.perfis}" />
					</p:selectOneRadio>
					<h:outputText></h:outputText>

					<h:outputLabel value="Login:" for="txtLogin" />

					<p:outputPanel id="customPanel">
						<p:selectOneRadio id="radioLogin" value="#{usuarioBean.selecao}"
							layout="custom">
							<f:selectItem itemValue="1" />
							<f:selectItem itemValue="2" />

						</p:selectOneRadio>
						<p:fieldset style="width:200px;height:60px">
							<h:panelGrid columns="2" id="oi">
								<p:radioButton id="opt1" for="radioLogin" itemIndex="0" />
								<h:outputLabel id="txtSugestao" for="opt1"
									value="#{usuarioBean.sugestao}" />

								<p:radioButton id="opt2" for="radioLogin" itemIndex="1" />
								<p:inputText for="opt2" id="txtLogin"
									value="#{usuarioBean.usuario.login}" />


							</h:panelGrid>
						</p:fieldset>
					</p:outputPanel>

					<br></br>
					<h:outputLabel value="Senha:" for="txtSenha" />
					<p:password id="txtSenha" value="#{usuarioBean.usuario.senha}">
						<f:validateLength minimum="3"></f:validateLength>
					</p:password>
					<br></br>
					<h:outputLabel value="Confirmação de Senha:" for="txtSenhaConfirma" />
					<p:password id="txtSenhaConfirma"
						value="#{usuarioBean.confirmaSenha}">
						<f:validateLength minimum="3"></f:validateLength>
					</p:password>
					<h:outputText></h:outputText>
					<br></br>
					<p:panel style="width:320px;height:50px;margin:0;">
						<p:commandButton value="Grava"
							actionListener="#{usuarioBean.grava}"
							oncomplete="handleCadastroRequest(xhr, status, args)"
							update="growl  :table:tabusr txtSenhaConfirma txtNome txtSobrenome 
							txtMatricula selectPerfil  txtSenha txtLogin txtSugestao customPanel" />

						<p:commandButton value="Voltar"
							actionListener="#{usuarioBean.limpar}"
							action="/usuario.xhtml?faces-redurect=true" ajax="false"
							immediate="true">

							<script type="text/javascript">
								function handleCadastroRequest(xhr, status,
										args) {
									if (args.validationFailed || !args.gravado) {
										jQuery('#popupCadastro').effect(
												"shake", {
													times : 3
												}, 100);
									} else {

										popupCadastro.hide();
									}
								}
							</script>
						</p:commandButton>

					</p:panel>

				</h:panelGrid>
			</p:dialog>
		</h:form>

		<h:form id="table">

			<p:dataTable id="tabusr" var="usr" style="margin-top: 5px"
				widgetVar="usrTable" value="#{usuarioBean.usuarios}"
				paginator="true" rows="7"
				rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
				rowIndexVar="rowIx" emptyMessage="Nenhum usuário cadastrado"
				filteredValue="#{usuarioBean.usuariosFiltrados}">
				<f:facet name="header">
					<h:outputText value="Grade Horaria"></h:outputText>
					<br></br>
					<br></br>
					<p:outputPanel>
						<h:outputText value="Pesquisar todos os campos " />
						<p:inputText id="globalFilter" onkeyup="usrTable.filter()" />
					</p:outputPanel>
				</f:facet>
				<p:columnGroup type="header">
					<p:row>
						<p:column colspan="1" headerText="Nome" filterBy="#{usr.nome}"
							filterMatchMode="contains" />
						<p:column colspan="1" headerText="Sobrenome"
							filterBy="#{usr.sobrenome}" filterMatchMode="contains" />
						<p:column colspan="1" headerText="Login" filterBy="#{usr.login}"
							filterMatchMode="contains" />
						<p:column colspan="1" headerText="Matricula"
							filterBy="#{usr.matricula}" filterMatchMode="contains" />
						<p:column colspan="1" headerText="Perfil" filterBy="#{usr.perfil}"
							filterMatchMode="exact"
							filterOptions="#{usuarioBean.opcaoPerfis}" />
						<p:column colspan="3" headerText="Ações" />
					</p:row>
				</p:columnGroup>

				<p:column>
						#{usr.nome}
					</p:column>

				<p:column>
						#{usr.sobrenome}
					</p:column>

				<p:column>
						#{usr.login}
					</p:column>
				<p:column>
						#{usr.matricula}
					</p:column>

				<p:column>
					<div align="center">#{usr.perfil}</div>
				</p:column>

				<p:column>
					<div align="center">
						<p:commandLink update=":formAltera:panelAltera" title="Editar"
							oncomplete="popupEdicao.show()">
							<f:setPropertyActionListener value="#{usr}"
								target="#{usuarioBean.usuario}" />
							<p:graphicImage value="resources/imagens/editar.png" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>

				<p:column>
					<div align="center">
						<p:commandLink update=":usrRemove:apaga" title="Remover"
							oncomplete="popupRemove.show()"
							rendered="#{loginBean.usuario.id != usr.id}">
							<f:setPropertyActionListener value="#{usr}"
								target="#{usuarioBean.usuario}" />
							<p:graphicImage value="resources/imagens/delete.png" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>

				<p:column>
					<div align="center">
						<p:commandLink update=":usrSenha:senha" title="Redefinir senha"
							oncomplete="popupSenha.show()"
							rendered="#{loginBean.usuario.id != usr.id}">
							<f:setPropertyActionListener value="#{usr}"
								target="#{usuarioBean.usuario}" />
							<p:graphicImage value="resources/imagens/senha.jpg" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>

			</p:dataTable>
		</h:form>

		<h:form id="formAltera">
			<p:dialog id="popupAltera" header="Edição de usuário"
				widgetVar="popupEdicao" closable="false" resizable="false"
				modal="true" hideEffect="slide" showEffect="clip">

				<h:panelGrid id="panelAltera" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Nome:" for="txtNome" />
					<p:inputText id="txtNome" value="#{usuarioBean.usuario.nome}" />


					<h:outputLabel value="Sobrenome:" for="txtSobrenome" />
					<p:inputText id="txtSobrenome"
						value="#{usuarioBean.usuario.sobrenome}" />

					<h:outputLabel value="Matricula:" for="txtMatricula" />
					<p:inputText id="txtMatricula"
						value="#{usuarioBean.usuario.matricula}" />

					<h:outputLabel value="Perfil:" for="selectPerfil" />
					<p:inputText id="selectPerfil"
						value="#{usuarioBean.usuario.perfil}" readonly="true" />


					<h:outputLabel value="Login:" for="txtLogin" />
					<p:inputText id="txtLogin" value="#{usuarioBean.usuario.login}" />


					<p:commandButton value="Salvar"
						actionListener="#{usuarioBean.atualiza}"
						oncomplete="handleCadastroRequest(xhr, status, args)"
						update=":cadForm:growl panelAltera :table:tabusr" />

					<p:commandButton value="Voltar" action="#{usuarioBean.limpar}"
						onclick="popupEdicao.hide()" ajax="true">

						<script type="text/javascript">
							function handleCadastroRequest(xhr, status, args) {
								if (args.validationFailed || !args.gravado) {
									jQuery('#popupEdicao').effect("shake", {
										times : 3
									}, 100);
								} else {

									popupEdicao.hide();
								}
							}
						</script>
					</p:commandButton>
				</h:panelGrid>
			</p:dialog>
		</h:form>

		<h:form id="usrRemove">
			<p:dialog header="Tem certeza?" widgetVar="popupRemove"
				resizable="false" id="apagUsuario" showEffect="fade"
				hideEffect="explode" closable="false" modal="true">

				<h:panelGrid id="apaga" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<p:commandButton value="Sim, remover"
						action="#{usuarioBean.remove}"
						update=":table:tabusr :cadForm:growl" onclick="popupRemove.hide()" />

					<p:commandButton value="Não remover" action="#{usuarioBean.limpar}"
						onclick="popupRemove.hide()" />
				</h:panelGrid>
			</p:dialog>
		</h:form>

		<h:form id="usrSenha">
			<p:dialog header="Redefinição de senha" widgetVar="popupSenha"
				resizable="false" id="senhaUsuario" showEffect="fade"
				hideEffect="explode" closable="false" modal="true">

				<h:panelGrid id="senha" columns="2" cellpadding="4"
					style="margin:0 auto;">
					<h:outputLabel value="Senha:" for="txtSenha" />

					<p:outputPanel id="alteraSenhaPanel">
						<p:selectOneRadio id="radioLogin" value="#{usuarioBean.selecao}"
							layout="custom">
							<f:selectItem itemValue="1" />
							<f:selectItem itemValue="2" />

						</p:selectOneRadio>
						<p:fieldset>
							<h:panelGrid columns="3" id="oi">
								<p:radioButton id="optS1" for="radioLogin" itemIndex="0" />
								<h:outputLabel value="Senha padrão"></h:outputLabel>
								<h:outputLabel id="outSenha" for="optS1"
									value="#{usuarioBean.senhaPadrao}" />

								<p:radioButton id="optS2" for="radioLogin" itemIndex="1" />
								<h:outputLabel value="Digitar senha"></h:outputLabel>
								<p:password for="optS2" id="optID"
									value="#{usuarioBean.novaSenha}" />
								<h:outputText></h:outputText>
								<h:outputLabel value="Confirmação de Senha:"></h:outputLabel>
								<p:password id="confSenha" value="#{usuarioBean.confirmaSenha}" />

							</h:panelGrid>
						</p:fieldset>
					</p:outputPanel>

					<p:commandButton value="Salvar" action="#{usuarioBean.alteraSenha}"
						update=":cadForm:growl"
						oncomplete="handleSenhaRequest(xhr, status, args)">

						<script type="text/javascript">
							function handleSenhaRequest(xhr, status, args) {
								if (args.validationFailed || !args.gravado) {
									jQuery('#popupSenha').effect("shake", {
										times : 3
									}, 100);
								} else {

									popupSenha.hide();
								}
							}
						</script>


					</p:commandButton>

					<p:commandButton value="Cancelar" action="#{usuarioBean.limpar}"
						onclick="popupSenha.hide()" />

				</h:panelGrid>
			</p:dialog>
		</h:form>
	</ui:define>
</ui:composition>
</html>